<?php
$user = "zhuguangbuyu@163.com";													//��¼�û���
$password = "1988313hcy";														//����


$loginURL = "http://www.kaixin001.com/login/login.php";						//��¼URL
$acc = $argv[1];
$login['url'] = "/";
//$login['invisible_mode'] = 0;
$login['email'] = $user;
$login['password'] = $password;
$login['remember'] = 1;														//Post ���
/*********************************
 ************** ��¼ **************
 *********************************/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $login);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$loginHeader = curl_exec($ch);
$header = explode("\r\n",$loginHeader);
$loginStat = "Login Error!!";
foreach ($header as $key => $value)
{
	if($value == "HTTP/1.1 302 Found")
	{
		$loginStat = "Login Success!!";
	}
	$headerRows = explode(": ",$value,2);
	if($headerRows[0] == "Set-Cookie")
	{
		$cookieRows = explode("; ",$headerRows[1],2);
		$cookie = explode("=",$cookieRows[0]);
		switch ($cookie[0]) {
			case "_user":
				$_user = $cookie[1];
				break;
			case "_email":
				$_email = $cookie[1];
				break;
			case "_kx":
				$_kx = $cookie[1];
				break;
			default:
				break;
		}
	}
}
echo $loginStat."\r\n";
$carsNum = 1;
while($carsNum != 0)
{
	/*********************************
	 *********** ��ȡverify ***********
	 *********************************/
	$cookie = "_user=".$_user."; _email=".$_email."; _kx=".$_kx.";";
	$homeURL = "http://www.kaixin001.com/app/app.php?aid=1040&url=index.php";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $homeURL);
	curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$homeHTML = curl_exec($ch);
	curl_close($ch);
	$arr = preg_match('/var g_verify = "(.+)";/',$homeHTML,$homeMatch);
	$verify = $homeMatch[1];
	if($verify != "")
	{
		echo "Get Verify Success !!\r\n";
	}
	/*********************************
	 ********** ��ȡ����Ϣ *********
	 *********************************/
	$arr = preg_match('/var v_userdata = (.+);/',$homeHTML,$carMatch);
	$carsInfo = $carMatch[1];
	$carsArray = json_decode($carsInfo);
	$ii = 0;
	unset($cars);
	foreach($carsArray->car as $cKey => $cValue)
	{
		if($cValue->park_profit >= 150)
		{
			$cars[] = $cValue->carid;
			$parkInfo[$ii]['uid'] = $cValue->park_uid;
			$parkInfo[$ii]['parkid'] = $cValue->parkid;
			$ii ++;
		}
		if($cValue->park_uid == 0)
		{
			$cars[] = $cValue->carid;
		}	
	}
	$carsNum = count($cars);
	echo $carsNum." cars need move !!\n";
	if($carsNum == 0)
	{
		exit();
	}
	/*********************************
	 ********** ��ȡ���ѳ�λ *********
	 *********************************/
	$arr = preg_match('/var v_frienddata = (.+);/',$homeHTML,$friendMatch);
	$friend = $friendMatch[1];
	$friendArray = json_decode($friend);
	$friendNum = count($friendArray);
	$j = 0;
	for($i = 0 ; $i < $friendNum ; $i++)
	{
		if($friendArray[$i]->full == 0)
		{
			$parkInfoURL = "http://www.kaixin001.com/parking/user.php";
			$data = array();
			$data['verify'] = $verify;
			$data['puid'] = $friendArray[$i]->uid;
			$data['_'] = '';
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $parkInfoURL);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$rs = json_decode(curl_exec($ch));
			foreach($rs->parking as $pKey => $pValue)
			{
				if(($pValue->carid == 0 || $pValue->carid == -1 ) && (($pValue->parkid >> 16) & 0xff) == 0 )
				{
					$parks[$j]['uid'] = $friendArray[$i]->uid;
					$parks[$j]['parkid'] = $pValue->parkid;
					$j++;
				}
			}
		}
		if($j == 10)
		{
			$i = $friendNum ;
		}
	}
	/*********************************
	 ********** �ϲ���λ��Ϣ *********
	 *********************************/
	if(!is_array($parks))
	{
		echo "no park to stop !!\r\n";
		exit();
	}
	$parks = array_merge($parks,$parkInfo);
	foreach ($cars as $v)
	{
		foreach ($parks as $key =>$vv)
		{
			sleep(10);
			$succeed = false;//����Ƿ����سɹ�
			$url = "http://www.kaixin001.com/parking/park.php";
			//3�����ػ��
			for ($i = 0; $i < 3 && $succeed == false; $i++)
			{
				$ch = curl_init();
				$data['carid'] = $v;
				$data['park_uid'] = $vv['uid'];
				$data['parkid'] = $vv['parkid'];			
				$data['verify'] = $verify;
				$data['acc'] = $acc;
				$data['neighbor'] = 0;
				$data['first_fee_parking'] = 0;
				$cookie = $cookie;
				//curl_setopt($ch, CURLOPT_HTTPHEADER, array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"));
				//curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest"));
				//curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Prototype-Version: 1.4.0"));
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_COOKIE, $cookie);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
				//curl_setopt($ch, CURLOPT_REFERER, "http://www.kaixin001.com/app/app.php?aid=1040"); 
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);			
				$rs = curl_exec($ch);
				if (!empty($rs))
				{
					$succeed = true;
				}
			}
			$content_arr = json_decode($rs);
			echo iconv("utf-8","gb2312",$content_arr->park_uid.":".$content_arr->parkid.":".$content_arr->carid.":".$content_arr->error	);
			echo "\n";
			if($content_arr->errno == 3)
			{
				$parks[] = $parks[$key];
				unset($parks[$key]);
			}
			if ($content_arr->errno == 0 || $content_arr->errno == 7 )
			{
				$parks[] = $parks[$key];
				unset($parks[$key]);
				break;
			}
		}
	}
}
?>